package cn.sinjet.myview;

import com.mgroup.wazehelper.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

public class MeterView extends ImageView{
	Drawable background = null;
	Drawable pointer = null;
	Bitmap scaledBg = null;
	Bitmap scalePointer = null;
	int pointerWidth = 0;
	int pointerHeight = 0;
	float rotateOffsetX = 0.0f;
	float rotateOffsetY = 0.0f;
	float pointerOffsetX = 0.0f;
	float pointerOffsetY = 0.0f;
	static final int INIT_ANGLE = -30;
//	Handler handler;
//	Runnable runnable;
	int angle = INIT_ANGLE;
	public MeterView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		try{
			TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.MeterView);
			background = typedArray.getDrawable(R.styleable.MeterView_meter_background);
			pointer = typedArray.getDrawable(R.styleable.MeterView_pointer);
			pointerOffsetX = typedArray.getDimension(R.styleable.MeterView_pointer_offset_x, 0.0f);
			pointerOffsetY = typedArray.getDimension(R.styleable.MeterView_pointer_offset_y, 0.0f);
			pointerWidth = (int)typedArray.getDimension(R.styleable.MeterView_pointer_width, 0);
			pointerHeight = (int)typedArray.getDimension(R.styleable.MeterView_pointer_height, 0);
			rotateOffsetX =  typedArray.getDimension(R.styleable.MeterView_rotate_offset_x, 0.0f);
			rotateOffsetY = typedArray.getDimension(R.styleable.MeterView_rotate_offset_y, 0.0f);
			typedArray.recycle();
			Log.d("draw","px");
		}catch(OutOfMemoryError err){
			err.printStackTrace();
		    System.gc();
		}
		/*handler = new Handler();
		runnable = new Runnable() {
			public void run() {
				// TODO Auto-generated method stub
				Log.d("draw","runnalbe meter"); 
				angle += 5;
				if(angle >360)  angle -= 360;
				invalidate();
				handler.postDelayed(runnable,500);
			}
		};*/
	}
	
	/*@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		//return super.onTouchEvent(event);
		switch(event.getAction()){
		case MotionEvent.ACTION_UP:
//			handler.postDelayed(runnable,500);
//			this.invalidate();
			break;
		}
		return true;
	}*/
//	 int x = 340;
//	 int y = 350;
	 Paint paint = null;
     @Override
    protected void onDraw(Canvas canvas) {
    	// TODO Auto-generated method stub
    	super.onDraw(canvas);
    	Log.d("draw", "meterview on draw");
    	//draw a background
    	if(paint == null){
    	   paint = new Paint();
    	   paint.setAntiAlias(true);
    	}
    	if(background == null || pointer == null){
    		Log.d("draw", "img == null,return");
    		return;
    	}
    	if(scaledBg == null){
    	   Bitmap bmBg = ((BitmapDrawable)background).getBitmap();
			try{
			   scaledBg = Bitmap.createScaledBitmap(bmBg, getWidth(), getHeight(), false);
			}catch(Exception e){
				e.printStackTrace();
				System.gc();
			}
    	}
		
    	if(scaledBg == null) return;
        
    	if(scalePointer == null){
            Bitmap bmPointer = ((BitmapDrawable)pointer).getBitmap();
            try{
            	scalePointer = Bitmap.createScaledBitmap(bmPointer,pointerWidth, pointerHeight, false);
            }catch(Exception e){
				e.printStackTrace();
				System.gc();
			}
    	}
    	if(scalePointer == null) return;
//    	int width = bmBg.getWidth();
//    	int height = bmBg.getHeight();
//        Log.d("draw", " x:"+x+" y:"+y+" width:"+width+" heigh:"+height);
    	canvas.drawBitmap(scaledBg, 0, 0,paint);
//    	canvas.save();
    	
    	canvas.translate(rotateOffsetX, rotateOffsetY);
//    	canvas.rotate(angle);
    	//draw a pointer
//    	 Matrix matrix = new Matrix();
//         matrix.postTranslate(rotateOffsetX, rotateOffsetY);
//    	 matrix.postTranslate(rotateOffsetX, rotateOffsetY);
//         matrix.setRotate(angle, pointerOffsetX, pointOffsetY);
//        canvas.drawBitmap(scalePointer, matrix, paint); 
    	canvas.rotate(angle);
    	canvas.drawBitmap(scalePointer,pointerOffsetX,pointerOffsetY,paint);
    	canvas.translate(-rotateOffsetX, -rotateOffsetY);
//    	canvas.restore();
    }
	public void setPointerDegree(int degree) {
		// TODO Auto-generated method stub
		angle = INIT_ANGLE+degree;
		this.invalidate();
	}

}
